﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class SystemUserBadgeController : DefaultController
	{
		public SystemUserBadgeController(IUserBadgeService userBadgeService)
		{
			this.UserBadgeService = userBadgeService;
		}

		public IUserBadgeService UserBadgeService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(int? page)
		{
			var badges = UserBadgeService.GetUsers(new UserBadgeSpecification
			{
				Page = page, 
				Limit = Settings.Users.BadgePageLimit
			});

			var badge = badges.FirstOrDefault();

			return badge.CanView ? View(Views.Index, Masters.System, badges) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Give()
		{
			var badge = UserBadgeService.Create();

			return badge.CanCreate ? View(Views.Give, Masters.System, new UserBadgeCreate { Existing = true }) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var badge = UserBadgeService.Create();

			return badge.CanCreate ? View(Views.Create, Masters.System, new UserBadgeCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(UserBadgeCreate value)
		{
			try
			{
				var badge = UserBadgeService.Create();

				if (!badge.CanCreate)
				{
					return NotAuthorized();
				}

				value.Source = Request.Files[0];

				this.UserBadgeService.Insert(badge, value);

				return Message(Routes.SystemUserBadgeUpdate, new { id = badge.Id }, Resources.Messages.UserBadgeCreated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View((value.Existing ? Views.Give : Views.Create), Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var badge = UserBadgeService.GetById(id);

			if (badge == null)
			{
				return Error(Resources.Messages.UserBadgeNotFound);
			}

			return badge.CanUpdate ? View(Views.Update, Masters.System, new UserBadgeUpdate(badge)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(UserBadgeUpdate value)
		{
			try
			{
				var badge = UserBadgeService.GetById(value.Id);

				if (badge == null)
				{
					return Error(Resources.Messages.UserBadgeNotFound);
				}

				if (!badge.CanUpdate)
				{
					return NotAuthorized();
				}

				value.Source = Request.Files[0];

				this.UserBadgeService.Update(badge, value);

				return Message(Routes.SystemUserBadgeUpdate, new { id = badge.Id }, Resources.Messages.UserBadgeUpdated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var badge = UserBadgeService.GetById(id);

			if (badge == null)
			{
				return Error(Resources.Messages.UserBadgeNotFound);
			}

			return badge.CanDelete ? View(Views.Delete, Masters.System, new UserBadgeDelete(badge)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(UserBadgeDelete value)
		{
			try
			{
				var badge = UserBadgeService.GetById(value.Id);

				if (badge == null)
				{
					return Error(Resources.Messages.UserBadgeNotFound);
				}

				if (!badge.CanDelete)
				{
					return NotAuthorized();
				}

				this.UserBadgeService.Delete(badge);

				return RedirectToRoute(Routes.SystemUserBadgeIndex);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Delete, Masters.System, value);
		}
	}
}